﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RecordTime.Data;
using RecordTime.Repository.Interfaces;

namespace RecordTime.Repository.Repositories
{
    public class TaskRepository : ITaskRepository
    {
        private readonly RecordingModelContainer _context;

        public TaskRepository(RecordingModelContainer context)
        {
            _context = context;
        }

        #region Implementation of IRepository<Task>

        public IEnumerable<Task> GetAll()
        {
            return _context.Tasks.ToList();
        }

        public Task GetById(int id)
        {
            return _context.Tasks.FirstOrDefault(x => x.TaskId.Equals(id));
        }

        public void Insert(Task task)
        {
            _context.Tasks.AddObject(task);
        }

        public void Edit(Task task)
        {
            _context.Tasks.Attach(task);
            _context.ObjectStateManager.ChangeObjectState(task, EntityState.Modified);
        }

        public void Delete(int id)
        {
            var task = GetById(id);
            _context.Tasks.DeleteObject(task);
        }

        #endregion
    }
}
